<?php
/**
 * Mastercontroller responsibilities:
 * select correct controller depending on input/url/session
 * merge output and generate page
 */

class MasterController {
	private $m_dbConnection = "";		//Dbconnection
	private $m_navigationView = "";
	private $m_memberController = "";
	private $m_loginController = "";
	private $m_observerController = "";
	private $m_logoutController = "";
	private $m_makeNewPostController ="";
	private $m_deleteMemberController = "";
	private $m_updatePostController = "";
	private $m_fileUploadController = "";

	public function __construct(DBConnection $a_dbConnection, 
								$a_navigationView,
								$a_memberController,
								$a_loginController,
								$a_observerController,
								$a_logoutController,
								$a_makeNewPostController,
								$a_deleteMemberController,
								$a_updatePostController,
								$a_fileUploadController) {
									
		$this->m_dbConnection = $a_dbConnection;
		$this->m_navigationView = $a_navigationView;
		$this->m_memberController = $a_memberController;
		$this->m_loginController = $a_loginController;
		$this->m_observerController = $a_observerController;
		$this->m_logoutController = $a_logoutController;
		$this->m_makeNewPostController = $a_makeNewPostController;
		$this->m_deleteMemberController = $a_deleteMemberController;
		$this->m_updatePostController = $a_updatePostController;
		$this->m_fileUploadController = $a_fileUploadController;
	}

	public function DoControl() {
		$pageCompositionView = new PageCompositionView();
		
		//Get xhtml output from active controller. 
		$controllerOutput = $this -> HandleActiveController($this->m_navigationView);
		
		//Let's make a menu that'll appear on every page. 
		$menu = $this->m_observerController->GetMenu();
		// Same for buttons. 
		$button = $this->m_loginController -> GetLoginOutButton();
		
		//Let's put all the parts together (menu, xhtml and so on) in our buildpage.
		return $pageCompositionView -> BuildPage($button, $menu, $controllerOutput);
	}

	//Which controller should we use? 
	private function HandleActiveController($a_navigationView) {		
		$userViews = $a_navigationView -> GetActiveControllerType();
		//Let's take the one that's active. 
		switch ($userViews) {
			case NavigationView::USER_VIEWS_BECOME_MEMBER : 
				$activeController = $this->m_memberController;
				break;
			
			case NavigationView::LOGIN_PAGE:
				$activeController = $this->m_loginController;
				break;
				
			case NavigationView::LOGOUT_PAGE:
				$activeController = $this->m_logoutController;
				break;
			
			case NavigationView::MAKE_NEW_POST_CONTROLLER:
				$activeController = $this->m_makeNewPostController;
				break;
			case NavigationView::DELETE_MEMBER__CONTROLLER:
				$activeController = $this->m_deleteMemberController;
				break;
			case NavigationView::UPDATE_POST:
				$activeController = $this->m_updatePostController;
				break;
			case NavigationView::FILE_UPLOAD_CONTROLLER:
				$activeController = $this->m_fileUploadController;
				break;
				
			// No active controller? let's have a default. 
			default:
				$activeController = $this->m_observerController;
				break;							
		}
		//The active controller generates output, great. 
		return $activeController -> DoControl();		
	}
}
